import { AboutPageType, adminConfig } from "@Tea/model"
import { renderSelectSql } from "../utils/renderSql"
import promisePool from "./index"
import { Response } from "express"
import { checkError } from "../utils/token"
import { SelectReturn } from "../types"
import { PositionManageContext } from "../context/PositionManageContext"
import { PositionRepository } from "./PositionRepository"

export class SystemRepository{
  public static inst = new SystemRepository()
  public static getInstance() {
    return SystemRepository.inst
  }

  /**
   * @use 获取菜单
   * @returns 
   */
  async getMune() {
    const data = {
      code: "base-web", title: "基础后台", children: [
        {
          code: "home", title: "系统首页", children: [
            {
              code: "systemBrowse", title: "系统一览", children: [
                {
                  code: "commitDetail", title: "提交详情", children: undefined
                },
                {
                  code: "test", title: "测试", children: undefined
                }
              ]
            }
          ]
        },
        {
          code: "personnelManage", title: "人员管理", children: [
            {
              code: "memberManage", title: "成员管理", children: [
                {
                  code: "memberList", title: "成员列表", children: undefined
                },
                {
                  code: "positionManage", title: "职位管理", children: undefined
                },
              ]
            },
            {
              code: "userManage", title: "用户管理",  children: [
                {
                  code: "userList", title: "用户列表", children: undefined
                },
              ]
            },
          ]
        },
        {
          code: "promotion", title: "测试组件", children: undefined
        },
        {
          code: "systemManage", title: "系统管理", children: [
            {
              code: "systemPowerMange", title: "系统权限管理",children:[
                { code: "menuManage", title: "菜单管理",children: undefined }
              ]
            },
            {
              code: "otherManage", title: "其它管理",children:[
                { code: "setting", title: "系统设置",children: undefined }
              ]
            }
          ]
        },
        {
          code: "about", title: "关于项目", children: undefined
        }
      ]
    }

    return data
  }

  async getSystemInfo(res: Response): Promise<AboutPageType> {
    
    const systemSql =  renderSelectSql({
      returnFeild: [
        {name:'online_time',aliasName:'onlineTime'}
      ],
      database: [
        {name:'system_info'}
      ],
      where: [{
        key: 'system_code',
        sqlVal: adminConfig.systemCode
      }]
    })

    const system = promisePool.query(systemSql) as unknown as SelectReturn<{ onlineTime: string }>

    const member = PositionRepository.getInstance().getPositionDetail({
      positionCode: "",
      positionName: "",
      memberName: "",
      currentPage: 1,
      pageSize:10000
    })
    
    return new Promise(resovle => {
      Promise.all([system, member]).then(res => {
        const data: AboutPageType = {
          onlineTime: res[0][0][0].onlineTime,
          memberList: []
        }
      
        res[1].data.forEach((e) => {
          const target = data.memberList.find(m => m.positionCode === e.positionCode)
          if (target) {
            target.list.push({ memberName: e.memberName })
          } else {
            data.memberList.push({
              positionCode: e.positionCode,
              positionName: e.positionName,
              list: [{ memberName: e.memberName }]
            })
          }
        })
      
        
        resovle(data)
      }).catch(e => {
        res.status(500).send({
          code: 0,
          msg: checkError(e)
        })
      })
    })
  }
}